<?php
	
	class UserDataProvider
    {
		private $db = NULL;
		
		public function __construct()
        {
			$this->db = DataBase::getInstance();
        }
	
		public function saveUserDataInDb( $firstName, $lastName, $email, $password, $sex, $month, $day, $year )
		{
			$query = " INSERT INTO user_list (first_name, last_name, email, password, sex, birth_month, birth_day, birth_year) VALUES ('".$this->db->quote( $firstName )."', '".$this->db->quote( $lastName )."', '".$this->db->quote( $email )."', '".$this->db->quote( $password )."', '".$this->db->quote( $sex )."', '".$this->db->quote( $month )."', '".$this->db->quote( $day )."', '".$this->db->quote( $year )."'); ";
			$this->db->query( $query );
		}
		
		public function saveFilenameInDb( $filename, $firstName, $lastName, $email )
		{
            $query = " INSERT INTO file_list ( filename, email, first_name, last_name, rating ) VALUES ( '".$this->db->quote( $filename )."', '".$this->db->quote( $email )."', '".$this->db->quote( $firstName )."', '".$this->db->quote( $lastName )."', 0 ); ";
            $this->db->query( $query );
		}		

		public function checkRepeatEmailInDb( $email )
		{
			$query = " SELECT DISTINCT * FROM user_list WHERE user_list.email='".$this->db->quote( $email )."'; ";
            $resultArr = $this->db->query( $query );
			if( !$resultArr )
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public function getEmailById( $userId )
		{
			$query = "SELECT email FROM user_list WHERE user_id=".$this->db->quote( $userId )."; ";
			$resultArr = $this->db->query( $query );
			if ( $resultArr )
			{
				return $resultArr[0]['email'];
			}
			else
			{
				return NULL;
			}
		}
		
		public function getUserDataFromDb( $email )
		{	
			$query = "SELECT DISTINCT * FROM user_list WHERE user_list.email='".$this->db->quote( $email )."'; ";
            $resultArr = $this->db->query( $query );
			if( $resultArr )
			{
				return $resultArr;
			}
			else
			{
				return NULL;
			}
		}
		
		public function checkLoginInDb( $email, $password )
		{	
			$query = "SELECT DISTINCT * FROM user_list WHERE user_list.email='".$this->db->quote( $email )."' AND user_list.password='".$this->db->quote( $password )."'; ";
			$resultArr = $this->db->query( $query );
			if( $resultArr )
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public function saveInfoFileInDb( $filename, $email, $firstName, $lastName )
		{
				$query = " INSERT INTO file_list ( filename, email, first_name, last_name, rating ) VALUES ('".$this->db->quote( $filename )."', '".$this->db->quote( $email )."', '".$this->db->quote( $firstName )."', '".$this->db->quote( $lastName )."', 0 ); ";
				$this->db->query( $query );
		}
		
		public function getEmailByFilename( $filename )
		{
			$query = "SELECT email FROM file_list WHERE filename='".$this->db->quote( $filename )."'; ";
			$resultArr = $this->db->query( $query );
			if ( $resultArr )
			{
				return $resultArr[0]['email'];
			}
			else
			{
				return NULL;
			}
		}
		
		public function getUserIdByEmail( $email )
		{
			$query = "SELECT user_id FROM user_list WHERE email='".$this->db->quote( $email )."'; ";
			$resultArr = $this->db->query( $query );
			if ( $resultArr )
			{
				return $resultArr[0]['user_id'];
			}
			else
			{
				return NULL;
			}
		}
		
		public function getFileIdByFilename( $filename )
		{
			$query = "SELECT files_id FROM file_list WHERE filename='".$this->db->quote( $filename )."'; ";
			$resultArr = $this->db->query( $query );
			if ( $resultArr )
			{
				return $resultArr[0]['files_id'];
			}
			else
			{
				return NULL;
			}
		}
		
		public function getUserListFromDb( $start, $num )
		{
			$query = "SELECT * FROM user_list LIMIT ".$start.",".$num." ";
			$resultArr = $this->db->query( $query );
	
			return $resultArr;
		}
		
		public function getAllUsersFromDb()
		{
			$query = "SELECT * FROM user_list; ";
			$resultArr = $this->db->query( $query );
	
			return $resultArr;
		}
		public function getNewRating( $fileId )
		{
			$query = "SELECT rating FROM file_list WHERE files_id=".$this->db->quote( $fileId )."; ";
			$resultArr = $this->db->query( $query );
			
			return intval( $resultArr[0]['rating'] );
		}

		public function getLastAction( $email, $fileId )
		{
			$query = "SELECT action FROM rating_changes WHERE files_id=".$this->db->quote( $fileId )." AND email='".$this->db->quote( $email )."'; ";
			$resultArr = $this->db->query( $query );
			if( !$resultArr )
			{
				return NULL;
			}
			else
			{
				return $resultArr[count( $resultArr )-1]['action'];
			}
		}

		public function updateRate( $newRate, $fileId )
		{
			$query = "UPDATE file_list SET rating=".$this->db->quote( $newRate )." WHERE files_id=".$this->db->quote( $fileId )."; ";
			$this->db->query( $query );
		}

		public function updateUserAction( $email, $fileId, $action )
		{
			$query = "INSERT INTO rating_changes(email, files_id, action) VALUES ('".$this->db->quote( $email )."', '".$this->db->quote( $fileId )."', '".$this->db->quote( $action )."');";
			$this->db->query( $query );
		}
		
	}
	
?>